<?php

class HmEvent_HmEventController extends Fw_Controller_Action
{
	public function init()
	{
		parent::init();
		$this->setTemplatePath('hm-event');
	}
	
	public function listAction()
	{
		$model      = $this->_getModel('HmEvent');
		$event_list = $this->_model;
		
		$key    = $this->_request->getParam('key');
		$value  = $this->_request->getParam('value');
		$query  = $event_list->select()->where($key.'="'.$value.'"');
		$rowset = $event_list->fetchAll($query);

		$rowcount = count($rowset);
		$this->view->event_list = $rowset;
		$this->view->rowcount   = $rowcount;
		$this->view->value      = $value;
	}
	
	public function nextAction()
	{
		$model      = $this->_getModel('HmEvent');
		$event_next = $this->_model;
		// $query      = $event_next->select()->limit('7');
		$rowset     = $event_next->fetchAll();

		$rowcount = count($rowset);
		$this->view->event_next = $rowset;
		$this->view->rowcount  = $rowcount;
	}
	
	public function showAction()
	{
		$event_id = (int)$this->_request->getParam('event_id');
				
		$model      = $this->_getModel('HmEvent');
		$event_show = $this->_model;

		$this->view->event_show = $event_show->fetchRow('event_id='.$event_id);
	}
	
	public function addAction()
	{
		if(!$this->initSession()) { return false; }
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('add');
			if ($formData) {
				
				$model = $this->_getModel('HmEvent');
				$add   = $this->_model;
				
				$row              = $add->createRow();
				$row->name        = $this->_request->getPost('name');
				$row->description = $this->_request->getPost('description');
				$row->event_begin = $this->_request->getPost('event_begin');
				$row->event_end   = $this->_request->getPost('event_end');
				$row->save();
				
				return $this->_helper->redirector('list');
			}
		}
	}
	
	public function editAction()
	{
		if(!$this->initSession()) { return false; }
		$model         = $this->_getModel('HmEvent');
		$event_edit    = $this->_model;
		$event_id      = (int)$this->_request->getParam('event_id');
		
		$row = $store_edit->fetchRow('event_id='.$event_id);
		
		if ($this->_request->isPost()) {
			$formData = $this->_request->getPost('edit');
			if ($formData) {
				$event_id         = (int)$this->_request->getPost('event_id');
				$row->name        = $this->_request->getPost('name');
				$row->description = $this->_request->getPost('description');
				$row->event_begin = $this->_request->getPost('event_begin');
				$row->event_end   = $this->_request->getPost('event_end');

				$row->save();
				
				return $this->_helper->redirector('list');
			}
		}
		$this->view->event_edit = $row;	
	}
	
	public function deleteAction()
	{
		if(!$this->initSession()) { return false; }
		$event_id = (int)$this->_request->getParam('event_id');
		if ($event_id > 0) {
			$model = $this->_getModel('HmEvent');
			$remove = $this->_model;
			
			$where = 'event_id = '. $event_id;
			$remove->delete($where);
		}
		return $this->_helper->redirector('list');
	}
}